#爬取斗图网的图片到本地

import requests
from lxml import etree
import re
from urllib3 import request

kv={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}

def getInfo(url):
    try:
        r=requests.get(url,headers=kv)
        r.encoding=r.apparent_encoding
        html=etree.HTML(r.text)
        div=html.xpath('//div[@class="random_picture"]')[0]
        imgs=div.xpath('.//img[contains(@class,"img-responsive")]')
        for img in imgs:
            imgUrl=img.get('data-backup')
            alt=img.get('alt')
            jpg=imgUrl.split('.')[-1]
            alt=re.sub(r'[\.。！，!?？, ]','',alt)
            filename=alt+'.'+jpg
            try:
                path='images/imgs/'+filename
                img_=requests.get(imgUrl)
                with open(path, 'wb') as f:
                    f.write(img_.content)
            except:
                print('下载图片%s出错'%filename)
    except:
        print('%s这个链接下载出错'%url)



def main(num=10):
    for x in range(1,10):
        url='http://www.doutula.com/photo/list/?page=%d'%x
        getInfo(url)

if __name__=='__main__':
    main()
